package com.example.wac.config;

import com.example.wac.filter.MyFilter;
import com.example.wac.interceptor.JwtTokenInterceptor;
import com.example.wac.interceptor.MdcInterceptor;
import com.example.wac.interceptor.MyArgumentResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.Filter;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author wuancheng
 * @description
 * @date 2021-07-13 14:06:00
 */

@Configuration
@Slf4j
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    JwtTokenInterceptor jwtTokenInterceptor;

    @Autowired
    MdcInterceptor mdcInterceptor;

    @Autowired
    MyFilter myFilter;

    @Autowired
    MyArgumentResolver myArgumentResolver;


    /**
     * 添加自定义token拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtTokenInterceptor)
                .addPathPatterns("/**")
                //获取token、刷新的token、获取登录验证码放行
                .excludePathPatterns("/token/token")
                .excludePathPatterns("/token/fresh_token")
                .excludePathPatterns("/user/get_code")
                .excludePathPatterns("/druid/*")
                .excludePathPatterns("/actuator");
        registry.addInterceptor(mdcInterceptor)
                .addPathPatterns("/**");
    }

    /**
     * 添加自定义过滤器
     * @return
     */
    @Bean
    public FilterRegistrationBean filterRegisterBean(){
        FilterRegistrationBean<Filter> filter = new FilterRegistrationBean<>();
        // 设置自定义过滤器
        filter.setFilter(myFilter);
        return filter;
    }

    /**
     * 参数解析器
     *
     * @param resolvers
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(myArgumentResolver);
    }

    /**
     * 信息转换器
     *
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.forEach(item->{
            if(item instanceof MappingJackson2HttpMessageConverter) {
                Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
//                builder.serializerByType(Number.class, new ToStringSerializer());
//                builder.serializationInclusion(JsonInclude.Include.NON_NULL);
                builder.dateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
                ((MappingJackson2HttpMessageConverter)item).setObjectMapper(builder.build());
            }
        });
    }
}
